import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
// import { Connection } from 'typeorm';

import { ArticleModule } from './modules/article/article.module';
import { UsersModule } from './modules/users/users.module';
import { RoleModule } from './modules/roles/role.module';
import { MinioModule } from './modules/minio/minio.module';
import { UploadModule } from './modules/file/file.module';
import { CommentModule } from './modules/comment/comment.module';
import { CategoryModule } from './modules/category/category.module';
import { SettingModule } from './modules/setting/setting.module';

import { TagModule } from './modules/tag/tag.module';
import { PassportModule } from '@nestjs/passport';
import { JwtModule } from '@nestjs/jwt';
import { AuthService } from './modules/auth/auth.service';
import { JwtStrategy } from './modules/auth/jwt.strategy';

import { ConfigModule, ConfigService } from '@nestjs/config'
import configuration from '../config/configuration'
import databaseconfig from '../config/database.config';

import * as Joi from '@hapi/joi';
@Module({
    imports: [
        ConfigModule.forRoot({
            //配置文件路径，也可以配置'.env'。
            envFilePath: ['/config/.env/.env.production', '/config/.env/.env.development'],
            //忽略配置文件，为true则仅读取操作系统环境变量，常用于生产环境
            ignoreEnvFile: false,
            //配置为全局可见，否则需要在每个模块中单独导入ConfigModule
            isGlobal: true,
            //导入自定义配置文件
            load: [configuration, databaseconfig],
            // 在实践中，为了保证代码可靠运行，需要对环境变量进行校验
            validationSchema:Joi.object({
                HOST:Joi.string().default('47.113.87.100'),
                ENV:Joi.string()
                .valid('development','production','test','show')
                .default('development')
              })
        }),
        // TypeOrmModule.forRoot()
        /**
         *  DATABASE_USER=root
            DATABASE_PASSWORD=root
            DATABASE_NAME=nest_blog
            DATABASE_PORT=3306
            DATABASE_HOST=localhost
         */
        TypeOrmModule.forRootAsync({
            imports:[ConfigModule],
            useFactory: (configService: ConfigService) => ({
                type: 'mysql',
                host: configService.get('database.host'),
                port: +configService.get<number>('database.port'),
                username: configService.get('database.username'),
                password: configService.get('database.password'),
                database: configService.get('database.database'),
                entities: [__dirname + '/**/*.entity{.ts,.js}'],
                synchronize: true,
              }),
            inject: [ConfigService]
        }),
        ArticleModule,
        UsersModule,
        RoleModule,
        TagModule,
        UploadModule,
        MinioModule,
        CommentModule,
        CategoryModule,
        SettingModule,
        PassportModule.register({ defaultStrategy: 'jwt' }),
        JwtModule.register({
            secret: 'secretKey',
            signOptions: {
                expiresIn: 3600,
            },
        })
    ],
    controllers: [AppController],
    providers: [AppService, AuthService, JwtStrategy]
})
export class AppModule {
    // constructor(private readonly connection: Connection) { }
}
